var util = require('../../utils/util.js');
var api = require('../../config/api.js');

var app = getApp()
Page({
  data: {
    keywrod: '',
    searchStatus: false,
    goodsList: [],
    helpKeyword: [],
    historyKeyword: [],
    categoryFilter: false,
    currentSortType: 'default',
    currentSortOrder: 'desc',
    filterCategory: [],
    defaultKeyword: {},
    hotKeyword: [],
    pageNum: 1,
     type:"",
    pageSize: 6,
    categoryId: 0
  },
  //事件处理函数
  closeSearch: function () {
    wx.navigateBack()
  },
  clearKeyword: function () {
    this.setData({
      keyword: '',
      searchStatus: false
    });
  },
  onLoad: function (options) {
    this.setData({
      typeId: options.typeId ? options.typeId : null,
      keyword: options.typeName ? options.typeName : null,
      type: wx.getStorageSync("type")
    });
    if (options.typeName) {
      wx.setNavigationBarTitle({
        title: options.typeName,
      })
    }
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winHeight: res.windowHeight
        })
      },
    })
  },
  onShow: function () {
    this.getSearchResult("");
  },
  getSearchKeyword() {
    let that = this;
    util.request(api.SearchIndex).then(function (res) {
      if (res.errno === 0) {
        that.setData({
          historyKeyword: res.data.historyKeywordList,
          defaultKeyword: res.data.defaultKeyword,
          hotKeyword: res.data.hotKeywordList
        });
      }
    });
  },

  inputChange: function (e) {

    this.setData({
      keyword: e.detail.value,
      searchStatus: false
    });
    // this.getHelpKeyword();
  },
  getHelpKeyword: function () {
    let that = this;
    util.request(api.SearchHelper, { keyword: that.data.keyword }).then(function (res) {
      if (res.errno === 0) {
        that.setData({
          helpKeyword: res.data
        });
      }
    });
  },
  inputFocus: function () {
    this.setData({
      searchStatus: false,
      goodsList: []
    });

    if (this.data.keyword) {
      this.getHelpKeyword();
    }
  },
  clearHistory: function () {
    this.setData({
      historyKeyword: []
    })

    util.request(api.SearchClearHistory, {}, 'POST')
      .then(function (res) {
        console.log('清除成功');
      });
  },
  getGoodsList: function () {
    let that = this;
    util.request(api.TourSearch, { selfSupport:true, keyword: that.data.keyword, pageNum: that.data.pageNum, pageSize: that.data.pageSize, orderType: that.data.currentSortType, isDesc: that.data.currentSortOrder, typeId: that.data.typeId }).then(function (res) {
      if (res.code == 200 && res.datas.length > 0) {
        that.setData({
          searchStatus: true,
          categoryFilter: false,
          goodsList: res.datas
        });
      }
      //重新获取关键词
      // that.getSearchKeyword();
    });
  },
  onKeywordTap: function (event) {

    this.getSearchResult(event.target.dataset.keyword);

  },
  getSearchResult(keyword) {

    this.setData({
      keyword: keyword,
      page: 1,
      categoryId: 0,
      goodsList: []
    });

    this.getGoodsList();
  },
  openSortFilter: function (event) {
    let currentId = event.currentTarget.id;
    switch (currentId) {
      case 'categoryFilter':
        this.setData({
          'categoryFilter': !this.data.categoryFilter,
          'currentSortOrder': 'asc'
        });
        break;
      case 'priceSort':
        let tmpSortOrder = 'asc';
        if (this.data.currentSortOrder == 'asc') {
          tmpSortOrder = 'desc';
        }
        this.setData({
          'currentSortType': 'price',
          'currentSortOrder': tmpSortOrder,
          'categoryFilter': false,
          pageNum:1
        });

        this.getGoodsList();
        break;
      default:
        //综合排序
        this.setData({
          'currentSortType': 'default',
          'currentSortOrder': 'desc',
          'categoryFilter': false
        });
        this.getGoodsList();
    }
  },
  selectCategory: function (event) {
    let currentIndex = event.target.dataset.categoryIndex;
    let filterCategory = this.data.filterCategory;
    let currentCategory = null;
    for (let key in filterCategory) {
      if (key == currentIndex) {
        filterCategory[key].selected = true;
        currentCategory = filterCategory[key];
      } else {
        filterCategory[key].selected = false;
      }
    }
    this.setData({
      'filterCategory': filterCategory,
      'categoryFilter': false,
      categoryId: currentCategory.id,
      page: 1,
      goodsList: []
    });
    this.getGoodsList();
  },
  onKeywordConfirm(event) {
    this.getSearchResult(event.detail.value);
  },
  fnShare: function (e) {
    var that = this;
    var path = "";
    var type = e.currentTarget.dataset.type;
    var goodsId = e.currentTarget.dataset.id;
    var imgUrl = e.currentTarget.dataset.url;
    var goodsName = e.currentTarget.dataset.name;
    var address = e.currentTarget.dataset.address;
    var price = e.currentTarget.dataset.price;
    var marketPrice = e.currentTarget.dataset.marketprice;
    if (type == 'hotel') {
      path = "/pages/hotelDetail/hoteldetail";
    } else if (type == "scene") {
      path = "/pages/sceneDetail/sceninfo";
    } else {
      path = "/pages/tourDetail/travelline"
    }
    app.fnShareGoods(path, goodsId, imgUrl, goodsName,type,address,price,marketPrice);
  }, 
  fnGetMore: function () {
    var that = this;
    util.request(api.TourSearch, { keyword: that.data.keyword, pageNum: that.data.pageNum, pageSize: that.data.pageSize, orderType: that.data.currentSortType, isDesc: that.data.currentSortOrder, typeId: that.data.typeId }).then(function (res) {
      if (res.code == "200") {
        if (res.datas && res.datas.length == that.data.pageSize) {
          //数据够显示一页
          that.setData({
            tourList: that.data.tourList.concat(res.datas),
            pageNum: that.data.pageNum + 1,
            noMore: false
          })
        } else if (res.datas && res.datas.length > 0) {
          //数据不够够显示一页
          that.setData({
            tourList: that.data.goodsList.concat(res.datas),
            pageNum: that.data.pageNum + 1,
            noMore: true
          })
        } else {
          
          that.setData({
            noMore: true
          })
        }
      }
    });
  }
})